home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter25 / isohex25_5 / isohex25_5.cpp < prev    next >
C/C++ Source or Header  |  2000-12-16  |  9KB  |  356 lines

  1. /*****************************************************************************
  2. IsoHex25_5.cpp
  3. Ernest S. Pazera
  4. 16DEC2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Needs ddraw.lib, d3d8.lib and dxguid.lib
  7. Needs GDICanvas.h/cpp
  8. Needs DDFuncs.h/cpp
  9. Needs D3DFuncs.h/cpp
  10. *****************************************************************************/
  11.  
  12. //////////////////////////////////////////////////////////////////////////////
  13. //INCLUDES
  14. //////////////////////////////////////////////////////////////////////////////
  15. #define WIN32_LEAN_AND_MEAN  
  16.  
  17. #include <windows.h>   
  18. #include <math.h>//sin and cos
  19. #include "GDICanvas.h"
  20. #include "ddraw.h"
  21. #include "DDFuncs.h"
  22. #include "d3d.h"
  23. #include "d3dfuncs.h"
  24.  
  25. //////////////////////////////////////////////////////////////////////////////
  26. //DEFINES
  27. //////////////////////////////////////////////////////////////////////////////
  28. //name for our window class
  29. #define WINDOWCLASS "ISOHEX25"
  30. //title of the application
  31. #define WINDOWTITLE "IsoHex 25-5"
  32.  
  33. //screen attributes
  34. const DWORD SCREENWIDTH=640;
  35. const DWORD SCREENHEIGHT=480;
  36. const DWORD SCREENBPP=16;
  37.  
  38. //tile dimensions
  39. const DWORD TILEWIDTH=64;
  40. const DWORD TILEHEIGHT=32;
  41.  
  42. //map dimensions
  43. const DWORD MAPWIDTH=20;
  44. const DWORD MAPHEIGHT=20;
  45.  
  46.  
  47. //////////////////////////////////////////////////////////////////////////////
  48. //PROTOTYPES
  49. //////////////////////////////////////////////////////////////////////////////
  50. bool Prog_Init();//game data initalizer
  51. void Prog_Loop();//main game loop
  52. void Prog_Done();//game clean up
  53.  
  54. //////////////////////////////////////////////////////////////////////////////
  55. //GLOBALS
  56. //////////////////////////////////////////////////////////////////////////////
  57. HINSTANCE hInstMain=NULL;//main application handle
  58. HWND hWndMain=NULL;//handle to our main window
  59.  
  60. //IDirectDraw7 Pointer
  61. LPDIRECTDRAW7 lpdd=NULL;
  62.  
  63. //surfaces
  64. LPDIRECTDRAWSURFACE7 lpddsPrime=NULL;
  65. LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
  66. LPDIRECTDRAWSURFACE7 lpddsTexture=NULL;
  67.  
  68. //IDirect3D7
  69. LPDIRECT3D7 lpd3d=NULL;
  70.  
  71. //IDirect3DDevice
  72. LPDIRECT3DDEVICE7 lpd3ddev=NULL;
  73.  
  74. //the height map
  75. D3DVALUE HeightMap[MAPWIDTH+1][MAPHEIGHT+1];
  76.  
  77. //vertices
  78. D3DTLVERTEX vert[4];//three vertices
  79.  
  80. //////////////////////////////////////////////////////////////////////////////
  81. //WINDOWPROC
  82. //////////////////////////////////////////////////////////////////////////////
  83. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  84. {
  85.     //which message did we get?
  86.     switch(uMsg)
  87.     {
  88.     case WM_KEYDOWN:
  89.         {
  90.             //check for escape key
  91.             if(wParam==VK_ESCAPE)
  92.             {
  93.                 DestroyWindow(hWndMain);
  94.             }
  95.  
  96.             return(0);//handled message
  97.         }break;
  98.     case WM_DESTROY://the window is being destroyed
  99.         {
  100.  
  101.             //tell the application we are quitting
  102.             PostQuitMessage(0);
  103.  
  104.             //handled message, so return 0
  105.             return(0);
  106.  
  107.         }break;
  108.     case WM_PAINT://the window needs repainting
  109.         {
  110.             //a variable needed for painting information
  111.             PAINTSTRUCT ps;
  112.             
  113.             //start painting
  114.             HDC hdc=BeginPaint(hwnd,&ps);
  115.  
  116.             /////////////////////////////
  117.             //painting code would go here
  118.             /////////////////////////////
  119.  
  120.             //end painting
  121.             EndPaint(hwnd,&ps);
  122.                         
  123.             //handled message, so return 0
  124.             return(0);
  125.         }break;
  126.     }
  127.  
  128.     //pass along any other message to default message handler
  129.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  130. }
  131.  
  132.  
  133. //////////////////////////////////////////////////////////////////////////////
  134. //WINMAIN
  135. //////////////////////////////////////////////////////////////////////////////
  136. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  137. {
  138.     //assign instance to global variable
  139.     hInstMain=hInstance;
  140.  
  141.     //create window class
  142.     WNDCLASSEX wcx;
  143.  
  144.     //set the size of the structure
  145.     wcx.cbSize=sizeof(WNDCLASSEX);
  146.  
  147.     //class style
  148.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  149.  
  150.     //window procedure
  151.     wcx.lpfnWndProc=TheWindowProc;
  152.  
  153.     //class extra
  154.     wcx.cbClsExtra=0;
  155.  
  156.     //window extra
  157.     wcx.cbWndExtra=0;
  158.  
  159.     //application handle
  160.     wcx.hInstance=hInstMain;
  161.  
  162.     //icon
  163.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  164.  
  165.     //cursor
  166.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  167.  
  168.     //background color
  169.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  170.  
  171.     //menu
  172.     wcx.lpszMenuName=NULL;
  173.  
  174.     //class name
  175.     wcx.lpszClassName=WINDOWCLASS;
  176.  
  177.     //small icon
  178.     wcx.hIconSm=NULL;
  179.  
  180.     //register the window class, return 0 if not successful
  181.     if(!RegisterClassEx(&wcx)) return(0);
  182.  
  183.     //create main window
  184.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  185.  
  186.     //error check
  187.     if(!hWndMain) return(0);
  188.  
  189.     //if program initialization failed, then return with 0
  190.     if(!Prog_Init()) return(0);
  191.  
  192.     //message structure
  193.     MSG msg;
  194.  
  195.     //message pump
  196.     for(;;)    
  197.     {
  198.         //look for a message
  199.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  200.         {
  201.             //there is a message
  202.  
  203.             //check that we arent quitting
  204.             if(msg.message==WM_QUIT) break;
  205.             
  206.             //translate message
  207.             TranslateMessage(&msg);
  208.  
  209.             //dispatch message
  210.             DispatchMessage(&msg);
  211.         }
  212.  
  213.         //run main game loop
  214.         Prog_Loop();
  215.     }
  216.     
  217.     //clean up program data
  218.     Prog_Done();
  219.  
  220.     //return the wparam from the WM_QUIT message
  221.     return(msg.wParam);
  222. }
  223.  
  224. //////////////////////////////////////////////////////////////////////////////
  225. //INITIALIZATION
  226. //////////////////////////////////////////////////////////////////////////////
  227. bool Prog_Init()
  228. {
  229.     lpdd=LPDD_Create(hWndMain,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT);
  230.  
  231.     //set the display mode
  232.     lpdd->SetDisplayMode(SCREENWIDTH,SCREENHEIGHT,SCREENBPP,0,0);
  233.  
  234.     //create primary surface
  235.     lpddsPrime=LPDDS_CreatePrimary3D(lpdd,1);
  236.  
  237.     //create back buffer
  238.     lpddsBack=LPDDS_GetSecondary3D(lpddsPrime);
  239.     
  240.     //get the idirect3d pointer
  241.     lpd3d=LPD3D_Create(lpdd);
  242.  
  243.     //create the idirect3ddevice(hack method)
  244.     lpd3ddev=LPD3DDEV_Create(lpd3d,lpddsBack);
  245.  
  246.     //set up viewport
  247.     LPD3DDEV_SetViewport(lpd3ddev,0,0,SCREENWIDTH,SCREENHEIGHT);
  248.  
  249.     //load in texture image
  250.     CGDICanvas gdic;
  251.     gdic.Load(NULL,"texture.bmp");
  252.  
  253.     //load texture
  254.     lpddsTexture=LPDDS_CreateTexture(lpdd,gdic.GetWidth(),gdic.GetHeight());
  255.  
  256.     //grab texture's DC
  257.     HDC hdc;
  258.     lpddsTexture->GetDC(&hdc);
  259.  
  260.     //blit image
  261.     BitBlt(hdc,0,0,gdic.GetWidth(),gdic.GetHeight(),gdic,0,0,SRCCOPY);
  262.  
  263.     //release texture's dc
  264.     lpddsTexture->ReleaseDC(hdc);
  265.  
  266.     //set this texture
  267.     lpd3ddev->SetTexture(0,lpddsTexture);
  268.  
  269.     //set up the height map
  270.     int x;
  271.     int y;
  272.     //loop through x
  273.     for(x=0;x<=MAPWIDTH;x++)
  274.     {
  275.         //loop through y
  276.         for(y=0;y<=MAPHEIGHT;y++)
  277.         {
  278.             //assign random value
  279.             HeightMap[x][y]=(float)(rand()%32);
  280.         }
  281.     }
  282.  
  283.     return(true);//return success
  284. }
  285.  
  286. //////////////////////////////////////////////////////////////////////////////
  287. //CLEANUP
  288. //////////////////////////////////////////////////////////////////////////////
  289. void Prog_Done()
  290. {    
  291.     //release texture
  292.     LPDDS_Release(&lpddsTexture);
  293.  
  294.     //release IDirect3DDevice
  295.     LPD3DDEV_Release(&lpd3ddev);
  296.  
  297.     //release IDirect3D 
  298.     LPD3D_Release(&lpd3d);
  299.  
  300.     //clean up primary surface(this will clean up the back buffer, also)
  301.     LPDDS_Release(&lpddsPrime);
  302.  
  303.     //clean up the dd pointer
  304.     LPDD_Release(&lpdd);
  305. }
  306.  
  307. //////////////////////////////////////////////////////////////////////////////
  308. //MAIN GAME LOOP
  309. //////////////////////////////////////////////////////////////////////////////
  310. void Prog_Loop()
  311. {
  312.     //clear the viewport to black
  313.     lpd3ddev->Clear(0,NULL,D3DCLEAR_TARGET,0,0,0);
  314.  
  315.     //start the scene
  316.     lpd3ddev->BeginScene();
  317.  
  318.     //center positions
  319.     D3DVALUE CenterX,CenterY;
  320.  
  321.     //loop through map
  322.     for(int y=0;y<MAPHEIGHT;y++)
  323.     {
  324.         for(int x=0;x<MAPWIDTH;x++)
  325.         {
  326.             //calculate world coordinates for center of tile
  327.             CenterX=(float)((x-y)*(TILEWIDTH/2)+320);
  328.             CenterY=(float)((x+y)*(TILEHEIGHT/2));
  329.  
  330.             //set up the vertex
  331.             //v1
  332.             VERTEX_Set(&vert[0],CenterX-TILEWIDTH/2,CenterY-HeightMap[x][y+1],D3DRGB(1.0,1.0,1.0),0.0,0.0);
  333.             vert[0].color=D3DRGB(0.5+HeightMap[x][y+1]/64.0,0.5+HeightMap[x][y+1]/64.0,0.5+HeightMap[x][y+1]/64.0);
  334.             //v2
  335.             VERTEX_Set(&vert[1],CenterX,CenterY-TILEHEIGHT/2-HeightMap[x][y],D3DRGB(1.0,1.0,1.0),1.0,0.0);
  336.             vert[1].color=D3DRGB(0.5+HeightMap[x][y]/64.0,0.5+HeightMap[x][y]/64.0,0.5+HeightMap[x][y]/64.0);
  337.             //v3
  338.             VERTEX_Set(&vert[2],CenterX,CenterY+TILEHEIGHT/2-HeightMap[x+1][y+1],D3DRGB(1.0,1.0,1.0),0.0,1.0);
  339.             vert[2].color=D3DRGB(0.5+HeightMap[x+1][y+1]/64.0,0.5+HeightMap[x+1][y+1]/64.0,0.5+HeightMap[x+1][y+1]/64.0);
  340.             //v4
  341.             VERTEX_Set(&vert[3],CenterX+TILEWIDTH/2,CenterY-HeightMap[x+1][y],D3DRGB(1.0,1.0,1.0),1.0,1.0);
  342.             vert[3].color=D3DRGB(0.5+HeightMap[x+1][y]/64.0,0.5+HeightMap[x+1][y]/64.0,0.5+HeightMap[x+1][y]/64.0);
  343.  
  344.             //render the triangle strip
  345.             LPD3DDEV_DrawTriangleStrip(lpd3ddev,vert,4);
  346.         }
  347.     }
  348.  
  349.     //end the scene
  350.     lpd3ddev->EndScene();
  351.  
  352.     //flip 
  353.     lpddsPrime->Flip(NULL,DDFLIP_WAIT);
  354. }
  355.  
  356.